﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
using System.Collections.Generic;
using System.Text;

namespace Order.EntityFrameworkCore
{
    public class DynamicModelCacheKeyFactory : IModelCacheKeyFactory
    {
        public object Create(DbContext context)
        {
           return new MyModelCacheKey(context);
        }
    }
    public class MyModelCacheKey : ModelCacheKey
    {
#pragma warning disable CS0649 // 从未对字段“MyModelCacheKey._schema”赋值，字段将一直保持其默认值 null
        string _schema;
#pragma warning restore CS0649 // 从未对字段“MyModelCacheKey._schema”赋值，字段将一直保持其默认值 null

        public MyModelCacheKey(DbContext context)
            : base(context)
        {
            
        }

        protected override bool Equals(ModelCacheKey other)
            => base.Equals(other)
                && (other as MyModelCacheKey)?._schema == _schema;

        public override int GetHashCode()
        {
            var hashCode = base.GetHashCode() * 397;
            if (_schema != null)
            {
                hashCode ^= _schema.GetHashCode();
            }

            return hashCode;
        }
    }
}
